//
//  SpeechController.m
//  iOS10Speech_OC
//
//  Created by qianjn on 2016/10/30.
//  Copyright © 2016年 SF. All rights reserved.
//

#import "SpeechController.h"
#import "XCSpeechRecognizer.h"
@interface SpeechController ()<XCSpeechRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView;

@property (weak, nonatomic) IBOutlet UIButton *btn;

@property (nonatomic, strong) XCSpeechRecognizer *speech;
@property(nonatomic,assign) NSInteger count;
@property(nonatomic,strong) NSString *tempStr;
@property(nonatomic,strong) NSString *string;
@property(nonatomic,strong) NSString *subfixStr;

@property(nonatomic,assign) NSInteger subfixLocation;

@property (assign, nonatomic) BOOL isAvailable;//

//subfix


@end

@implementation SpeechController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.speech = [[XCSpeechRecognizer alloc] initWithLocaleIdentifier:@"zh-CN"];
    self.speech.delegate = self;
    
    self.count = 0;
    self.isAvailable = YES;
    self.subfixLocation = 0;
    
}



- (IBAction)startRecording:(id)sender {
    
    if ([self.speech isRunning]) {
        [self.speech stopRecording];
        [self.btn setTitle:@"开始录音" forState:UIControlStateNormal];
    } else {
        [self.speech startRecording];
        [self.btn setTitle:@"停止录音" forState:UIControlStateNormal];
    }
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.speech stopRecording];
}


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.count = 0;
    self.isAvailable = YES;
    self.subfixLocation = 0;
}


#pragma mark - XCSpeechRecognizerDelegate
- (void)recognizeDidStart:(NSError *)error
{
    NSLog(@"recognizeDidStart-----error%@", error);
}
- (void)recognizeDidStop
{
    //    UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"录音结束" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    //    [view show];
    
}
- (void)recognizeFail:(NSError *)error
{
    NSLog(@"recognizeFail -----fail%@", error);
}
- (void)recognizeSuccess:(NSString *)res
{
    NSString *result = [res stringByReplacingOccurrencesOfString :@" " withString:@""];
    
    self.string =@"";
    NSRange range;
    
    NSArray *list =@[@"嘿小度",@"嘿小杜",@"嘿小肚",@"嘿小猪",@"嘿小兔",@"嘿，小度",@"嘿，小杜",@"嘿，小肚",@"嘿，小猪",@"嘿，小兔",@"小杜",@"小度",@"小猪",@"小兔"];

    for (NSUInteger i = 0; i <list.count; i++) {
        
        NSLog(@"--外---result===%@", result);
        
        if ([result  containsString:list[i]]) {
            range = [result rangeOfString:list[i]];
            NSLog(@"found at location = %lu, length = %lu",(unsigned long)range.location,(unsigned long)range.length);
            self.string = [result  substringWithRange:range];
            NSLog(@"-----self.string===%@", self.string);
            
            if( self.isAvailable){
                self.subfixLocation = range.location+range.length;
                
                UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"" message:nil delegate:nil cancelButtonTitle:self.string otherButtonTitles:nil];
                [view show];
                self.isAvailable = NO;
            }
            [_textView setText:[NSString stringWithFormat:@"---%@", result]];
        } else {
            
            if(self.subfixLocation == 0) {
                [_textView setText:[NSString stringWithFormat:@"---%@", result]];
            }else {
                
                if (self.subfixLocation > result.length) {
                    self.subfixLocation  = result.length;
                }
                
                self.subfixStr = [result  substringFromIndex:self.subfixLocation]; ;
                [_textView setText:[NSString stringWithFormat:@"---%@", self.subfixStr]];
            }
        }
    }
}
- (void)recognizeFinish
{
    
}
@end
